//
//  ChatListTableViewCell.m
//  Findif
//
//  Created by Mac on 2018/11/6.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "ChatListTableViewCell.h"
#import "JCHATStringUtils.h"
#import "JCHATSendMsgManager.h"

@interface ChatListTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * iconImageView;
//@property (nonatomic, weak)UIImageView * shopImageView;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * contentLabel;
@property (nonatomic, weak)UILabel * unreadLabel;

@end

@implementation ChatListTableViewCell


- (void)setModel:(MessageNotiModel *)model{
    
    _model = model;
    
    self.iconImageView.image = [UIImage imageNamed:@"社团通知"];
//    self.shopImageView.hidden = YES;
//    self.shopImageView.width = 0;
    self.nameLabel.text = @"社团通知";
    self.timeLabel.text = model.inputtime;
    
    int type = [model.inform_type intValue];
    
    if (type == 1) {
        
        self.contentLabel.text = [NSString stringWithFormat:@"%@申请加入%@",model.user_name,model.group_name];
    }else if (type == 2){
        
        self.contentLabel.text = [NSString stringWithFormat:@"社长%@已解散%@",model.user_name,model.group_name];
    }else if (type == 3){
        
        self.contentLabel.text = [NSString stringWithFormat:@"你被%@移除%@",model.user_name,model.group_name];
    }else if (type == 4){
        
        self.contentLabel.text = [NSString stringWithFormat:@"%@邀请你加入%@",model.user_name,model.group_name];
    }else if (type == 5){
        
        self.contentLabel.text = [NSString stringWithFormat:@"%@拒绝你加入%@",model.user_name,model.group_name];
    }else{
        
        self.contentLabel.text = @"没什么新鲜事~~";
    }
    
}


- (void)setUnreadCount:(NSString *)unreadCount{
    
    _unreadCount = unreadCount;
    
    int count = [unreadCount intValue];
    if (count == 0) {
        
        self.unreadLabel.hidden = YES;
    }else if (count > 99){
        
        self.unreadLabel.hidden = NO;
        self.unreadLabel.text = @" 99+ ";
    }else{
        
        self.unreadLabel.hidden = NO;
        self.unreadLabel.text = [NSString stringWithFormat:@"%d  ",count];
    }
}

- (void)setConversation:(JMSGConversation *)conversation{
    
    _conversation = conversation;
    int conversationType = conversation.conversationType;
    if (conversationType == 1) {
        //单聊
        JMSGUser *user = conversation.target;
        
//        NSLog(@"conversation===%@",conversation);
//        NSLog(@"JMSGUser===%@",user);
        //    NSLog(@"nickname===%@",user.nickname);
        ////    NSLog(@"unreadCount===%@",conversation.unreadCount);
        
        if (user.nickname) {
            self.nameLabel.text = user.nickname;
        }else{
            self.nameLabel.text = conversation.title;
        }
        //    NSLog(@"username==%@",user.username);
        
        [conversation avatarData:^(NSData *data, NSString *objectId, NSError *error) {
            
            if (error == nil) {
                if (data != nil) {
                    
                    [self.iconImageView setImage:[UIImage imageWithData:data]];
                } else {
                    if (conversation.conversationType == kJMSGConversationTypeSingle) {
                        
                        [self.iconImageView setImage:[UIImage imageNamed:@"组12"]];
                    }
                }
            }else{
                
                NSLog(@"error===%@",error);
            }
        }];
        
        if (conversation.latestMessage.timestamp != nil ) {
            double time = [conversation.latestMessage.timestamp doubleValue];
            self.timeLabel.text = [JCHATStringUtils getFriendlyDateString:time forConversation:YES];
        } else {
            self.timeLabel.text = @"";
        }
        
        
        self.contentLabel.text = conversation.latestMessageContentText;
        
        int count = [conversation.unreadCount intValue];
        if (count == 0) {
            
            self.unreadLabel.hidden = YES;
        }else if (count > 99){
            
            self.unreadLabel.hidden = NO;
            self.unreadLabel.text = @" 99+ ";
        }else{
            
            self.unreadLabel.hidden = NO;
            self.unreadLabel.text = [NSString stringWithFormat:@"%d  ",count];
        }
    }else if (conversationType == 2){
        //群聊
        self.nameLabel.text = conversation.title;
//        self.shopImageView.hidden = YES;
//        self.shopImageView.width = 0;
        
        if (conversation.latestMessage.timestamp != nil ) {
            double time = [conversation.latestMessage.timestamp doubleValue];
            self.timeLabel.text = [JCHATStringUtils getFriendlyDateString:time forConversation:YES];
        } else {
            self.timeLabel.text = @"";
        }
        
        
        self.contentLabel.text = conversation.latestMessageContentText;
        
        int count = [conversation.unreadCount intValue];
        if (count == 0) {
            
            self.unreadLabel.hidden = YES;
        }else if (count > 99){
            
            self.unreadLabel.hidden = NO;
            self.unreadLabel.text = @" 99+ ";
        }else{
            
            self.unreadLabel.hidden = NO;
            self.unreadLabel.text = [NSString stringWithFormat:@"%d  ",count];
        }
        
        [conversation avatarData:^(NSData *data, NSString *objectId, NSError *error) {
            
            if (error == nil) {
                if (data != nil) {
                    
                    [self.iconImageView setImage:[UIImage imageWithData:data]];
                } else {
                    if (conversation.conversationType == kJMSGConversationTypeSingle) {
                        
                        [self.iconImageView setImage:[UIImage imageNamed:@"组12"]];
                    }
                }
            }else{
                
                NSLog(@"error===%@",error);
            }
        }];
    }
    
//    self.unreadLabel.text = [NSString stringWithFormat:@"%@",conversation.unreadCount];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xEFF0F1);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 5;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.image = [UIImage imageNamed:@"组12"];
    iconImageView.layer.cornerRadius = 25 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    
    self.iconImageView = iconImageView;
    
    [self.backView addSubview:self.iconImageView];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor = ZRBlackColor;
    nameLabel.text = @"用户姓名";
    nameLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    
    self.nameLabel = nameLabel;
    
    [self.backView addSubview:self.nameLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.text = @"09-09";
    timeLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    timeLabel.textAlignment = NSTextAlignmentRight;
    
    self.timeLabel = timeLabel;
    
    [self.backView addSubview:self.timeLabel];
    
    
    UILabel * contentLabel = [UILabel new];
    
    contentLabel.textColor = [UIColor lightGrayColor];
    contentLabel.text = @"你好，很高兴认识你，方便聊一下吗？";
    contentLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.contentLabel = contentLabel;
    
    [self.backView addSubview:self.contentLabel];
    
    
    UILabel * unreadLabel = [UILabel new];
    
    unreadLabel.textColor = ZRWhiteColor;
    unreadLabel.backgroundColor = [UIColor redColor];
    unreadLabel.text = @"--";
    unreadLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    unreadLabel.textAlignment = NSTextAlignmentCenter;
    unreadLabel.layer.cornerRadius = 5 * SCREENH6;
    unreadLabel.layer.masksToBounds = YES;
    unreadLabel.hidden = YES;
    
    self.unreadLabel = unreadLabel;
    
    [self.backView addSubview:self.unreadLabel];
}


-(void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(80 * SCREENH6);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
        make.width.height.mas_equalTo(50 * SCREENH6);
    }];
    
//    [self.shopImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(10 * SCREENH6);
//        make.top.mas_equalTo(self.iconImageView);
//        make.width.mas_equalTo(27 * SCREENH6);
//        make.height.mas_equalTo(14 * SCREENH6);
//    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView);
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(10 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self.backView).offset(300 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.nameLabel);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.trailing.mas_equalTo(self.backView).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.unreadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.iconImageView);
        make.trailing.mas_equalTo(self.timeLabel);
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
